package com.yoyosys.echo.manager.cluster.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yoyosys.echo.manager.cluster.dao.EExtLineage2Mapper;
import com.yoyosys.echo.manager.system.common.Message;
import com.yoyosys.echo.manager.cluster.dao.ExchangeTaskMapper;
import com.yoyosys.echo.manager.cluster.po.EExtLineage;
import com.yoyosys.echo.manager.cluster.vo.Dependency;
import com.yoyosys.echo.manager.cluster.po.ExchangeTask;
import com.yoyosys.echo.manager.cluster.service.ExchangeTaskService;
import com.yoyosys.echo.manager.cluster.vo.ExchangeTaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service("thirdExchangeTaskService")
public class ExchangeTaskServiceImpl implements ExchangeTaskService {
    @Autowired
    private ExchangeTaskMapper exchangeTaskMapper;
    @Autowired
    private EExtLineage2Mapper eExtLineageMapper;
    @Override
    public Message jobinfo(ExchangeTaskVo exchangeTask) {
        Message message = new Message();
     try {
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         String data = sdf.format(new Date());
         ExchangeTask exchange = new ExchangeTask();
         exchange.setId(exchangeTask.getId());
         exchange.setUnitCode(exchangeTask.getUnitCode());
         exchange.setLabel(exchangeTask.getLabel());
         exchange.setDescr(exchangeTask.getDescr());
         exchange.setBatchType(exchangeTask.getBatchType());
         exchange.setCreateUser(exchangeTask.getCreateUser());
         exchange.setCreateDate(data);
         exchange.setTeamName(exchangeTask.getTeamId());
         exchange.setTeamLabel(exchangeTask.getTeamLabel());
         exchange.setLastEditor(exchangeTask.getLastEditor());
         exchange.setLastUpdateDate(data);
         exchange.setRequestId(exchangeTask.getRequestId());
         // 处理依赖关系
         List<EExtLineage> eExtLineages=new ArrayList<>();
         if (exchangeTask.getDependencys() != null) {
             List<Dependency> dependencies = exchangeTask.getDependencys().stream()
                     .map(dto -> {
                         List<EExtLineage> eExtLineageInfo=EExtLineageInfo(dto, exchangeTask);
                         eExtLineages.addAll(eExtLineageInfo);
                         Dependency dependency = new Dependency();
                         dependency.setFroms(dto.getFroms());
                         dependency.setJobId(dto.getJobId());
                         dependency.setOptionDesc(dto.getOptionDesc());
                         dependency.setTo(dto.getTo());
                         dependency.setTo_system(dto.getTo_system());
                         return dependency;
                     })
                     .collect(Collectors.toList());
             ObjectMapper mapper = new ObjectMapper();
             String json = mapper.writeValueAsString(dependencies);
             exchange.setDependencys(json);
         }
         ExchangeTask exchangetask=exchangeTaskMapper.selectJobInfo(exchangeTask.getId());
         if(exchangetask==null){
             exchangeTaskMapper.jobInfo(exchange);
         }else {
             exchangeTaskMapper.updateJobInfo(exchange);
             eExtLineageMapper.delExtLineage(exchangeTask.getId());
         }
         eExtLineageMapper.insrteExtLineage(eExtLineages);
         message.setSuccessMsg("任务提交成功");
         message.setData(exchange.getRequestId());
         message.setCode("200");
     }catch (Exception e){
         message.setErrorMsg("任务提交失败");
         message.setCode("500");
         message.setData(e.getMessage());
     }
        return message;
    }

    private List<EExtLineage> EExtLineageInfo(Dependency dto, ExchangeTaskVo exchangeTask) {
        List<EExtLineage> eExtLineages = new ArrayList<>();

        if (dto.getFroms() != null && !dto.getFroms().isEmpty()){
            List<EExtLineage> collect = dto.getFroms().stream().map(from -> {
                EExtLineage extLineage = new EExtLineage();
                String[] split = from.split("\\.");
                extLineage.setFromDbName(split[0]);
                extLineage.setFromTableName(split[1]);
                extLineage.setJobId(exchangeTask.getId());
                extLineage.setJobName(exchangeTask.getLabel());
                extLineage.setFromDbType("dataFuse");
                extLineage.setFromApp("dataFuse");
                extLineage.setToApp("dataFuse");
                extLineage.setToDbType(dto.getTo_system());
                extLineage.setSourceType("EXTERNAL");
                extLineage.setSourceApp("sjdz_gongxiangpingtai");
                extLineage.setCreateTime(new Date());
                extLineage.setUpdateTime(new Date());
                extLineage.setJobDesc(exchangeTask.getDescr());
                extLineage.setSourceTeamId(exchangeTask.getTeamId());
                extLineage.setSourceTeamName(exchangeTask.getTeamLabel());
                if (dto.getTo() != null && !dto.getTo().isEmpty() && dto.getTo().contains(".")) {
                    String[] splitTo = dto.getTo().split("\\.");
                    extLineage.setToDbName(splitTo[0]);
                    extLineage.setToTableName(splitTo[1]);
                }
                return extLineage;
            }).collect(Collectors.toList());
            eExtLineages.addAll(collect);
        }else{
            EExtLineage extLineage = new EExtLineage();
            extLineage.setJobId(exchangeTask.getId());
            extLineage.setJobName(exchangeTask.getLabel());
            extLineage.setFromDbType("dataFuse");
            extLineage.setFromApp("dataFuse");
            extLineage.setToApp("dataFuse");
            extLineage.setSourceType("EXTERNAL");
            extLineage.setSourceApp("sjdz_gongxiangpingtai");
            extLineage.setCreateTime(new Date());
            extLineage.setUpdateTime(new Date());
            extLineage.setToDbType(dto.getTo_system());
            extLineage.setJobDesc(exchangeTask.getDescr());
            extLineage.setSourceTeamId(exchangeTask.getTeamId());
            extLineage.setSourceTeamName(exchangeTask.getTeamLabel());
            if (dto.getTo() != null && !dto.getTo().isEmpty() && dto.getTo().contains(".")) {
                String[] splitTo = dto.getTo().split("\\.");
                extLineage.setToDbName(splitTo[0]);
                extLineage.setToTableName(splitTo[1]);
            }
            eExtLineages.add(extLineage);
        }

        return eExtLineages;
    }



}
